
/*
Irving Derin:
 This is a simple sketch that allows for the transmission of 9 bytes to a recieving 
 coordinator. The recieving address for this coordinator is "0x0010", this is the 
 ATMY value that can be set either using a serial connection directly to the boards 
 that are going to be transmitting. 
 
 
 */

#include <SoftwareSerial.h>  //Serial Library
#include <stdlib.h>
#include <XBee.h>            //Xbee Library
XBee xbee = XBee();  // Creates XBee object 


uint8_t payload[6];                   

Tx16Request tx = Tx16Request(0x0010, payload, sizeof(payload)); // Creation of the object to be sent

int blah = random(21);

const int V1 = A0; //Voltage at cathode 1
const int V2 = A1; //Voltage of cathode 2


int Voltage1 = 0;  //Voltage of cell 1 (mV)
int Voltage2 = 0;  //Voltage of cell 2 (mV)

void setup(){
  //Serial.begin(9600);
  xbee.begin(9600);
  Serial.begin(9600);
  pinMode(V1, INPUT);
  pinMode(V2, INPUT);
}


void loop(){

  Voltage1 = 0;
  Voltage2 = 0;
  for(int x=0; x<10; x++)
  {  
    Voltage1 = (analogRead(V1)+Voltage1);
    delay(10);
    Voltage2 = (analogRead(V2)+Voltage2);
    delay(10);
  }
  Voltage1 = Voltage1/10;
  Voltage2 = Voltage2/10;

  if (Voltage1 < .1)
  {
    Voltage1 = Voltage1*30;
  }
  else if (Voltage1 < 2.5)
  {
    Voltage1 = (5.7143*Voltage1+9.7143);
  }
  else if(Voltage1 >= 2.5 && Voltage1 <=200)
  {
    Voltage1 = (4.8947*Voltage1+11.539);
  }
  else if (Voltage1 >= 201 && Voltage1 <= 395)
  {
    Voltage1 = (4.89*Voltage1+13.362);
  }
  else
  {
    Voltage1 = (4.885*Voltage1+12.158);
  }



  if (Voltage2 < .1)
  {
    Voltage2 = Voltage2*30;
  }
  else if (Voltage2 < 2.5)
  {
    Voltage2 = (5.7143*Voltage2+9.7143);
  }
  else if(Voltage2 >= 2.5 && Voltage2 <=200)
  {
    Voltage2 = (4.8947*Voltage2+11.539);
  }
  else if (Voltage2 >= 201 && Voltage2 <= 395)
  {
    Voltage2 = (4.88*Voltage2+13.362);
  }
  else
  {
    Voltage2 = (4.885*Voltage2+12.158);
  }

  Serial.print("Voltage 1: ");
  Serial.println(Voltage1);
  Serial.print("Voltage 2: ");
  Serial.println(Voltage2);
  for(int i = 0 ; i < sizeof(payload) ; i++){
    Serial.print("Value of array payload[");
    Serial.print(i);
    Serial.print("] is equal too: ");
    Serial.println(payload[i]);
  }
  

  payload[0] = Voltage1 >> 8 & 0xff;
  payload[1] = Voltage1 & 0xff;
  payload[2] = Voltage2 >> 8 & 0xff;
  payload[3] = Voltage2 & 0xff;
  payload[4] = 123;
  payload[5] = 67;
  
  delay(1000);
  xbee.send(tx);
}




